<template>
	<view class="home">
		<view class="header">
			<view class="my">
				<image @click="toggle(type)" class="img" src="../../static/assets/my-list.png" mode=""></image>
			</view>

			<!-- 普通弹窗 -->
			<uni-popup ref="popup" background-color="#ffffff" @change="change">
				<view class="popup-content"
					:class="{ 'popup-height': type === 'left' || type === 'right' || type === 'center' }">
					<text class="text">
						<image v-if= "user_img.length == 0" @click="goLoginFn" class="img" src="/static/assets/my-title01.png" mode=""></image>
						<image v-else class="img" :src="user_img" mode=""></image>
						<ul class="list">
							<li>{{currentTime}}</li>
							<li>昵称：{{userName}}</li>
							<li>收货地址</li>
							<li>我的订单</li>
							<li>我的收藏</li>
							<li>我的优惠券</li>
						</ul>
					</text>
				</view>
			</uni-popup>

			<navigator url="../search/search">
				<view class="search">
					<image class="img" src="../../static/assets/search-icon.png" mode=""></image>
					<input type="text" placeholder-style="color: #f0f0f0" placeholder="请输入关键字" />
				</view>
			</navigator>
		</view>

		<view class="section">
			<view class="title">
				<h3>时尚服装</h3>
				<h2>衣梦空间，任你挑选</h2>
			</view>

			<view class="wrap">
				<view class="recommend">
					<view class="box-bg" v-for="(item, index) in icon_arr" :key="item.id" @click="selectItem(index)"
						:class="{ 'active': index === selectedIndex}">
						<view v-if="item.id === 1" :class="{ 'hot': true, 'active': selectedIndex === index }">
							{{ item.text }}
						</view>
						<image v-else class="img"
							:src="selectedIndex === index ? item.recommend_icon_on : item.recommend_icon" mode="">
						</image>
					</view>
				</view>
			</view>
		</view>

		<view class="wrap-show">
			<view class="content" id="slider">
				<!-- 使用数组渲染 -->
				<view class="content-bg" v-for="(item, index) in items" :key="index"
					:class="{ active: currentIndex === index }">
					<view class="text">
						<image class="img" :src="item.img" mode=""></image>
						<h3>{{ item.title }}</h3>
						<view class="money-box">
							<view class="left">
								<h2>{{ item.price }}</h2>
								<view class="hot-good">
									<view class="hot">
										<p>{{ item.hot }}</p>
									</view>
									<view class="good">
										<p>{{ item.good }}</p>
									</view>
								</view>
							</view>
							<view @click="toggleCart(item)" :class="{'right': true, 'active': item.isActive}">
								<image class="img" src="../../static/assets/cart-add.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<div class="container">
			<ul>
				<li class="list" v-for="(product,index) in products" :key="index">
					<view class="list-left">
						<image class="img" :src="product.products_img" mode=""></image>
					</view>
					<view class="list-right">
						<h3>{{ product.products_title }}</h3>
						<p>{{ product.products_description }}</p>
						<view class="price-buy">
							<view class="price">
								<h3>￥<span>{{ product.products_price }}</span>RMB</h3>
							</view>
							<view class="buy">
								<button class="btn" @click="payFn(index)">立即购买</button>
							</view>
						</view>
					</view>
				</li>

			</ul>
		</div>

	</view>
</template>

<script>
	import api from '../../api';
	export default {
		data() {
			return {
				type: 'left',
				 // 点击索引值，默认为0
				selectedIndex: 0,
				// 用户名
				userName: "未登录~~",
				// 获取头像
				user_img: "",
				// 定义时间变量
				currentTime: "",
				// 购物车高亮
				isActive: false,
				icon_arr: [{
						id: 1,
						text: "热门推荐"
					},
					{
						id: 2,
						recommend_icon: "../../static/assets/re_man.png",
						recommend_icon_on: "../../static/assets/re_man_on.png",
					},
					{
						id: 3,
						recommend_icon: "../../static/assets/re_women.png",
						recommend_icon_on: "../../static/assets/re_women_on.png",
					},
					{
						id: 4,
						recommend_icon: "../../static/assets/re_shoe.png",
						recommend_icon_on: "../../static/assets/re_shoe_on.png",
					},
					{
						id: 5,
						recommend_icon: "../../static/assets/re_socks.png",
						recommend_icon_on: "../../static/assets/re_socks_on.png",
					},
					{
						id: 6,
						recommend_icon: "../../static/assets/re_bag.png",
						recommend_icon_on: "../../static/assets/re_bag_on.png",
					}
				],

				items: [{
						recommend_id: 1,
						img: 'https://h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/653458/2024/1023/163/8f6313c9-b687-4bb4-a06f-b5875e83d609_282x282_90.jpg',
						title: '法式高级感名媛风慵懒气质优雅收腰V领连衣裙',
						price: '￥116.46',
						hot: '热销榜NO.11',
						good: '热销榜NO.1',
					},
					{
						recommend_id: 2,
						img: 'https://img.alicdn.com/imgextra/i4/2216690523529/O1CN01E4L99t1bwKiO6MHLa_!!2216690523529.jpg_.webp',
						title: '好物甄选春秋款上新丨情侣款开衫',
						price: '￥98',
						hot: '热销榜NO.8',
						good: '热销榜NO.3'
					},
					{
						recommend_id: 3,
						img: 'https://img.alicdn.com/imgextra/i3/2208135750202/O1CN01K2bN111DMYsPEgZU9_!!2208135750202.jpg_.webp',
						title: '艺术生高级感针织开衫外套温柔风宽松浅蓝色海马毛毛衣女秋冬外穿',
						price: '￥79',
						hot: '热销榜NO.5',
						good: '热销榜NO.12'
					},
				],

				products: [{
						products_id: 1,
						products_img: 'https://h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/639307/2024/0228/98/7224c4ef-a712-435e-a1c5-d6c8e18c1389_262x262_90.jpg',
						products_title: '2024秋季圆领卫衣青年爆款印花内搭打底衫潮流时尚套头上衣男',
						products_description: '【时尚帅气】秋季男士运动百搭潮流舒适长袖上衣男式打底衫卫衣',
						products_price: 59
					},
					{
						products_id: 2,
						products_img: 'https://img.alicdn.com/imgextra/i3/839582537/O1CN01eJpEpR1UbzohOvo77_!!0-item_pic.jpg_.webp',
						products_title: '五公主高领蕾丝打底衫新款秋冬时尚洋气妈妈大码黑色内搭加绒上衣',
						products_description: '【性感洋气】高领蕾丝打底衫新款秋冬时尚洋气妈妈大码黑色内搭加绒上衣',
						products_price: 128
					},
					{
						products_id: 3,
						products_img: 'https://img.alicdn.com/imgextra/i1/2216040523590/O1CN01j3mj111cOGsHzVUGq_!!2216040523590.jpg_.webp',
						products_title: '2024冬季奥莱官方高端羽绒服男士秋冬加厚鸭绒休闲面包服连帽户外潮流外套',
						products_description: '【时尚帅气】冬季高端羽绒服男士秋冬加厚鸭绒休闲面包服连帽户外潮流外套',
						products_price: 518
					},
					{
						products_id: 4,
						products_img: 'https://img.alicdn.com/imgextra/i2/652548433/O1CN01klI7UK2CAMwuarsKb_!!652548433.jpg_.webp',
						products_title: '重工水洗做旧帆布旅行袋复古男士飞行员手提包二战军风单肩斜跨包',
						products_description: '【时尚复古】帆布旅行袋复古男士飞行员手提包',
						products_price: 328
					},

				],
			}
		},
		methods: {
			toggle(type) {
				this.type = type;
				this.$refs.popup.open(type);
			},
			selectItem(index) {
				this.selectedIndex = index; // 记录点击的索引值
			},
			payFn(pay_id) {
				// 将products对象序列化为JSON字符串
				const productData = JSON.stringify(this.products)
				uni.navigateTo({
					url: `/pages/pay/pay?pay_id=${pay_id}&productData=${encodeURIComponent(productData)}`
				})
			},
			getUserInfo() {
				let name = uni.getStorageSync("NICKNAME");
				if(name){
				this.userName = name;
				}
			},
			// 获取当前时间方法
			setCurrentTime() {
				// 获取当前时间
				const now = new Date();
				// 获取年、月、日、时、分、秒
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始
				const day = String(now.getDate()).padStart(2, '0');
				const hours = String(now.getHours()).padStart(2, '0');
				const minutes = String(now.getMinutes()).padStart(2, '0');
				const seconds = String(now.getSeconds()).padStart(2, '0');
				// 格式化时间字符串
				this.currentTime = `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
			},
			// 获取用户信息
			async getUserInfoFn() {
				let token = uni.getStorageSync("AUTOTOKEN");
				// console.log(token);
				if (token) {
					api.getUserInfo({token})
						.then(
							content => {
								// console.log(content);
								let {code,result} = content;
								if (code == 200) {
									let title_img = api.host + result[0].imgUrl;
									this.user_img = title_img;
									// this.value = result[0].nickName;
								}
							}
						)
				}
			},
			// 点击头像跳转到登录页
			goLoginFn(){
				uni.showModal({
					title: '提示',
					content: '是否要登录账号？',
					confirmText: "是",
					cancelText: "否",
					success: (res) => {
						if (res.confirm) {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 1000)
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			// 切换该商品的购物车高亮状态
			toggleCart(item) {
				let token = uni.getStorageSync("AUTOTOKEN");
				if(!token){
					uni.showModal({
						title: "提示",
						content: "您还没登录，请登录！~~",
						success: (res) => {
							if (res.confirm) {
								setTimeout(() => {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}, 1000)
							} else if (res.cancel) {
								// console.log('用户点击取消');
							}
						}
					})
				}
				else{
					if(!item.isActive){
						setTimeout(()=>{
							uni.showToast({
								title: "商品添加成功",
								icon: "success"
							},1000)
						})
					}else{
						// 取消添加购物车
						// console.log("取消添加到购物车");
					}
					item.isActive = !item.isActive;
				}
				// console.log(item);
			},
		},
		onLoad() {
			this.getUserInfo();
			setInterval(()=>{
				this.setCurrentTime();
			},1000);
			this.getUserInfoFn();
		},
		onShow() {
			this.getUserInfoFn();
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../uni.scss';

	ul {
		margin: 0;
		padding: 0;
	}

	li {
		list-style: none;
	}


	.home {
		width: 100%;

		.header {
			padding: $pad;
			// width: 690rpx;
			height: 160rpx;
			line-height: 160rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.text {
				width: 100%;
				margin: 0;
				text-align: center;

				.img {
					width: 150rpx;
					height: 150rpx;
					border-radius: 50%;
				}

				.list {
					// padding: $pad;
					// width: 100%;
					display: flex;
					justify-content: center;
					flex-direction: column;
					text-align: center;
					margin-bottom: 250rpx;

					li {
						margin-top: -30rpx;
						display: flex;
						align-items: center;
						flex-direction: column;
						height: 150rpx;
						border-bottom: 2rpx solid #666666;
						font-size: 30rpx;
						padding-top: 20rpx;
					}
				}
			}

			.my {
				.img {
					width: 80rpx;
					height: 80rpx;
					vertical-align: middle;
				}
			}

			.search {
				width: 365rpx;
				padding: $pad;
				height: 90rpx;
				box-sizing: border-box;
				background: $bgRight;
				box-shadow: 10rpx 10rpx 20rpx 5rpx #e8c8c8;
				border-radius: 50rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.img {
					width: 50rpx;
					height: 50rpx;
					vertical-align: middle;
				}

				input {
					width: 250rpx;
					padding: 0 15rpx;
					box-sizing: border-box;
				}
			}
		}

		.section {
			color: $color;
			padding: $pad;

			.title {
				padding-top: 10rpx;
				margin-bottom: 10rpx;
			}

			.wrap {
				width: 100%;
				padding: 10rpx 0;
				overflow: scroll;
				box-sizing: border-box;

				.recommend {
					margin-top: 20rpx;
					width: fit-content;
					display: flex;
					flex-wrap: nowrap;
					box-sizing: border-box;

					.box-bg {
						width: 130rpx;
						height: 130rpx;
						background-color: #f8ebec;
						text-align: center;
						border-radius: 20rpx;
						margin-right: 30rpx;

						&.active {
							background: $bgBottom;
							box-shadow: 2rpx 10rpx 15rpx #e8cdcd;
						}

						.hot {
							padding: 17rpx 20rpx;
							color: #666666; // 默认颜色
							font-size: 35rpx;

							&.active {
								color: #ffffff; // 点击后的高亮颜色
							}
						}

						.img {
							width: 70rpx;
							height: 70rpx;
							padding-top: 30rpx;
						}
					}
				}
			}
		}

		.wrap-show {
			padding: 10rpx 20rpx;
			overflow: scroll;
			box-sizing: border-box;

			.content {
				width: fit-content;
				display: flex;
				flex-wrap: nowrap;
				padding-right: 20rpx;
				box-sizing: border-box;
				padding-bottom: 20rpx;

				.content-bg {
					width: 320rpx;
					// height: 430rpx;
					background: $bgTop;
					margin-top: 20rpx;
					margin-right: 25rpx;
					border-radius: 20rpx;
					box-shadow: 10rpx 10rpx 15rpx $boxShadow;

					.text {
						color: #ffffff;
						width: 100%;
						text-align: center;

						.img {
							padding-top: 10rpx;
							width: 250rpx;
							height: 260rpx;
						}

						h3 {
							padding: 0 20rpx;
							text-align: left;
							font-size: 35rpx;
							overflow: hidden;
							/* 超出隐藏 */
							display: -webkit-box;
							/* 设置为 flexbox */
							-webkit-box-orient: vertical;
							/* 垂直显示 */
							-webkit-line-clamp: 1;
							/* 显示的行数 */
							text-overflow: ellipsis;
							/* 超出部分显示省略号 */
						}

						.money-box {
							display: flex;
							padding: 0 15rpx;
							margin-top: 15rpx;
							justify-content: space-between;
							padding-bottom: 20rpx;
							align-items: center;

							.left {
								text-align: center;
								width: calc(100% - 100rpx);

								h2 {
									text-align: left;
									font-size: 40rpx;
								}
							}

							.hot-good {
								font-size: 15rpx;
								width: 210rpx;
								margin-top: 10rpx;
								display: flex;

								.hot {
									padding: 5rpx;
									margin-right: 10rpx;
									border: 2rpx solid #fff;
									border-radius: 10rpx;
								}

								.good {
									padding: 5rpx;
									border: 2rpx solid #fff;
									border-radius: 10rpx;
								}
							}

							.right {
								width: 70rpx;
								height: 70rpx;
								background-color: #ff989d;
								border: 2rpx solid #fff;
								border-radius: 50%;
								// box-shadow: 0 2rpx 5rpx #ccc;
								&.active{
									background-color: $bagroundColor;
									box-shadow: 0 2rpx 5rpx #ccc;
								}

								.img {
									width: 30rpx;
									height: 30rpx;
									padding-top: 20rpx;
								}
							}
						}

					}
				}
			}
		}

		.container {
			margin-top: 30rpx;
			padding: $pad;

			ul {
				width: 100%;

				li.list {
					background-color: #f5f3f3;
					border-radius: 20rpx;
					padding: 20rpx 30rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 20rpx;
					box-shadow: 0 0rpx 8rpx #e8c8c8;

					.list-left {
						width: 220rpx;
						height: 200rpx;
						background-color: #666666;

						.img {
							width: 100%;
							height: 100%;
						}
					}

					.list-right {
						width: 400rpx;

						h3 {
							font-size: 32rpx;
							color: #414141;
							overflow: hidden;
							/* 超出隐藏 */
							display: -webkit-box;
							/* 设置为 flexbox */
							-webkit-box-orient: vertical;
							/* 垂直显示 */
							-webkit-line-clamp: 1;
							/* 显示的行数 */
							text-overflow: ellipsis;
							/* 超出部分显示省略号 */
						}

						p {
							padding-top: 10rpx;
							font-size: 25rpx;
							color: #a5a5a5;
							margin-bottom: 10rpx;
							overflow: hidden;
							/* 超出隐藏 */
							display: -webkit-box;
							/* 设置为 flexbox */
							-webkit-box-orient: vertical;
							/* 垂直显示 */
							-webkit-line-clamp: 2;
							/* 显示的行数 */
							text-overflow: ellipsis;
							/* 超出部分显示省略号 */
						}

						.price-buy {
							display: flex;
							align-items: center;
							justify-content: space-between;
							// box-shadow: $boxShadow;

							.price {
								h3 {
									color: $color;
									font-weight: normal;

									span {
										font-size: 45rpx;
										font-weight: bold;
									}
								}
							}

							.buy {
								.btn {
									background: $bgBottom;
									font-size: 30rpx;
									border-radius: 50rpx;
									color: #f5f3f3;
									height: 60rpx;
									line-height: 60rpx;
								}
							}
						}
					}
				}

			}
		}
	}

	// 隐藏横向滚动条
	::-webkit-scrollbar {
		display: none;
	}

	@mixin flex {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	@mixin height {
		/* #ifndef APP-NVUE */
		height: 100%;
		/* #endif */
		/* #ifdef APP-NVUE */
		flex: 1;
		/* #endif */
	}

	.popup-content {
		@include flex;
		align-items: center;
		justify-content: center;
		padding: 15px;
		height: 50px;
		background-color: #fff;
	}

	.popup-height {
		@include height;
		width: 200px;
	}
</style>